/**
 * 
 */
package com.binhthuan.catechist.client.component;

import com.smartgwt.client.types.AnimationEffect;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Dialog;
import com.smartgwt.client.widgets.events.CloseClickHandler;
import com.smartgwt.client.widgets.events.CloseClientEvent;
import com.smartgwt.client.widgets.form.DynamicForm;

/**
 * @author dttgiang
 * 
 */
public class PopupDialog extends Dialog {

    private boolean useAnimation;

    public PopupDialog() {
        this(false);
    }

    public PopupDialog(boolean useAnimation) {
        this(null);
        this.useAnimation = useAnimation;
    }

    public PopupDialog(String title) {
        setTitle(title);
        setIsModal(true);
        setShowModalMask(true);
        setShowMinimizeButton(false);
        setDismissOnEscape(true);
        setAutoSize(true);
        centerInPage();
        setShowToolbar(false);
        setCanDragReposition(true);
        setAnimateTime(500);
        setAnimateShowEffect(AnimationEffect.FADE);
        addCloseClickHandler(new CloseClickHandler() {

            @Override
            public void onCloseClick(CloseClientEvent event) {
                closeButtonClick();
            }
        });
    }

    protected void closeButtonClick() {
        for (Canvas c : getItems()) {
            if (c instanceof DynamicForm) {
                ((DynamicForm) c).reset();
            }
        }

        if (useAnimation) {
            animateHide(AnimationEffect.FADE);
        } else {
            hide();
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.smartgwt.client.widgets.Canvas#hide()
     */
    @Override
    public void hide() {
        if (!useAnimation) {
            super.hide();
        } else {
            animateHide(AnimationEffect.FADE);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.smartgwt.client.widgets.Canvas#show()
     */
    @Override
    public void show() {
        if (!useAnimation) {
            super.show();
        } else {
            animateShow(AnimationEffect.FADE);
        }
    }
}
